package com.cyt.wx.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.wx.dao.WxIndustryCategoryDao;
import com.cyt.wx.model.po.WxIndustryCategory;
import com.cyt.wx.model.vo.WxIndustryCategoryVo;
import com.cyt.wx.service.WxIndustryCategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 官网行业分类-官网行业分类Service层实现类
 *
 * @author congcong tao
 * @date 2019-8-13 9:57:02
 */
@Service("wxIndustryCategoryService")
public class WxIndustryCategoryServiceImpl extends BaseServiceImpl<WxIndustryCategory> implements WxIndustryCategoryService {

    @Resource
    private WxIndustryCategoryDao wxIndustryCategoryDao;

    @Autowired
    public void setBaseDao(WxIndustryCategoryDao wxIndustryCategoryDao) {
        this.baseDao = wxIndustryCategoryDao;
    }

    /**
     * 查询对象集合
     *
     * @param wxIndustryCategoryVo 对象
     * @return List 对象集合
     * @author congcong tao
     * @date 2019-8-13 9:57:02
     */
    @Override
    public List<WxIndustryCategory> listExample(WxIndustryCategoryVo wxIndustryCategoryVo) {
        Example example = new Example(WxIndustryCategory.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();
        if (StrUtil.isNotEmpty(wxIndustryCategoryVo.getName())) {
            criteria.andLike("name", "%" + wxIndustryCategoryVo.getName() + "%");
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        return wxIndustryCategoryDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param wxIndustryCategoryVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-13 9:57:02
     */
    @Override
    public ReturnResult page(WxIndustryCategoryVo wxIndustryCategoryVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<WxIndustryCategory> list = listExample(wxIndustryCategoryVo);
        PageInfo<WxIndustryCategory> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param wxIndustryCategoryVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-13 9:57:02
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(WxIndustryCategoryVo wxIndustryCategoryVo, SysUser sysUser) {
        if (StrUtil.isEmpty(wxIndustryCategoryVo.getId())) {
            WxIndustryCategory wxIndustryCategory = new WxIndustryCategory(sysUser);
            wxIndustryCategory.setName(wxIndustryCategoryVo.getName());
            wxIndustryCategory.setPhoto(wxIndustryCategoryVo.getPhoto());
            wxIndustryCategory.setUrl(wxIndustryCategoryVo.getUrl());
            wxIndustryCategory.setDescription(wxIndustryCategoryVo.getDescription());
            wxIndustryCategory.setStatus(wxIndustryCategoryVo.getStatus());
            wxIndustryCategory.setIndexStatus(wxIndustryCategoryVo.getIndexStatus());
            wxIndustryCategory.setOrderNum(wxIndustryCategoryVo.getOrderNum());
            int num = wxIndustryCategoryDao.insert(wxIndustryCategory);
            if (num > 0) {
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            WxIndustryCategory wxIndustryCategory = wxIndustryCategoryDao.selectByPrimaryKey(wxIndustryCategoryVo.getId());
            wxIndustryCategory.setName(wxIndustryCategoryVo.getName());
            wxIndustryCategory.setPhoto(wxIndustryCategoryVo.getPhoto());
            wxIndustryCategory.setUrl(wxIndustryCategoryVo.getUrl());
            wxIndustryCategory.setDescription(wxIndustryCategoryVo.getDescription());
            wxIndustryCategory.setStatus(wxIndustryCategoryVo.getStatus());
            wxIndustryCategory.setIndexStatus(wxIndustryCategoryVo.getIndexStatus());
            wxIndustryCategory.setOrderNum(wxIndustryCategoryVo.getOrderNum());
            wxIndustryCategory.setUpdateTime(new Date());
            wxIndustryCategory.setUpdateUser(sysUser.getId());
            int num = wxIndustryCategoryDao.updateByPrimaryKey(wxIndustryCategory);
            if (num > 0) {
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param wxIndustryCategoryVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-13 9:57:02
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(WxIndustryCategoryVo wxIndustryCategoryVo, SysUser sysUser) {
        //要修改的字段
        WxIndustryCategory wxIndustryCategory = new WxIndustryCategory();
        wxIndustryCategory.setRemoveTag(BaseConstant.REMOVE_TAG_YES);
        wxIndustryCategory.setUpdateUser(sysUser.getId());
        wxIndustryCategory.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(WxIndustryCategory.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wxIndustryCategoryVo.getId())) {
            criteria.andIn("id", Arrays.asList(wxIndustryCategoryVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        wxIndustryCategoryDao.updateByExampleSelective(wxIndustryCategory, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param wxIndustryCategoryVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-13 9:57:02
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(WxIndustryCategoryVo wxIndustryCategoryVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(WxIndustryCategory.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wxIndustryCategoryVo.getId())) {
            criteria.andIn("id", Arrays.asList(wxIndustryCategoryVo.getId().split(",")));
        }
        wxIndustryCategoryDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }
}